package com.czp.infosharingplatformapp.service.impl;

import com.czp.infosharingplatformapp.model.Post;
import com.czp.infosharingplatformapp.model.PostTag;
import com.czp.infosharingplatformapp.model.UserPostRecord;
import com.czp.infosharingplatformapp.repository.PostRepository;
import com.czp.infosharingplatformapp.repository.PostTagRepository;
import com.czp.infosharingplatformapp.repository.UserPostRecordRepository;
import com.czp.infosharingplatformapp.service.UserPostRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 基于以下规则进行用户兴趣建模：
 *   用户对标签的喜好权重：如果用户多次浏览包含同一个标签的帖子，则权重会随着浏览次数增加。
 *   用户对分类的整体兴趣：如果用户多次浏览某个分类下的帖子，则该分类的兴趣值也会升高。
 * 权重值可以通过以下公式简单统计：
 *   标签权重 = 某个用户浏览的帖子中，标签的出现次数。
 *   分类权重 = 某个用户浏览的帖子中，分类的出现次数。
 * 这些权重将作为协同过滤算法的输入。
 *
 */

@Service
public class UserPostRecordServiceImpl implements UserPostRecordService {

    @Autowired
    private UserPostRecordRepository userPostRecordRepository;

    @Autowired
    private PostRepository postRepository;

    @Autowired
    private PostTagRepository postTagRepository;

    @Override
    public void recordView(Long userId, Long postId) {
        UserPostRecord record = new UserPostRecord();
        record.setUserId(userId);
        record.setPostId(postId);
        record.setViewedAt(LocalDateTime.now());
        userPostRecordRepository.save(record);
    }

    @Override
    public Map<String, Integer> computeUserTagPreferences(Long userId) {
        List<UserPostRecord> records = userPostRecordRepository.findByUserId(userId);
        Map<String, Integer> tagWeights = new HashMap<>(); // 标签权重统计

        for (UserPostRecord record : records) {
            Long postId = record.getPostId();
            List<PostTag> tags = postTagRepository.findByPostId(postId); // 获取帖子的标签

            for (PostTag tag : tags) {
                tagWeights.put(tag.getTag(), tagWeights.getOrDefault(tag.getTag(), 0) + 1);
            }
        }

        return tagWeights;
    }

    @Override
    public Map<String, Integer> computeUserCategoryPreferences(Long userId) {
        List<UserPostRecord> records = userPostRecordRepository.findByUserId(userId);
        Map<String, Integer> categoryWeights = new HashMap<>(); // 分类权重统计

        for (UserPostRecord record : records) {
            Long postId = record.getPostId();
            Post post = postRepository.findById(postId).orElseThrow(() -> new RuntimeException("帖子不存在"));
            String category = post.getCategory().getName(); // 假设 Post 有分类信息

            categoryWeights.put(category, categoryWeights.getOrDefault(category, 0) + 1);
        }

        return categoryWeights;
    }

    @Override
    public List<UserPostRecord> getUserRecords(Long userId) {
        return userPostRecordRepository.findByUserId(userId); // 调用 Repository 查询浏览记录
    }

    @Override
    public boolean hasUserViewedPost(Long userId, Long postId) {
        return userPostRecordRepository.existsByUserIdAndPostId(userId, postId);
    }
}